package com.dwd.hrm.controller;

import com.dwd.hrm.doc.CourseDoc;
import com.dwd.hrm.domain.CourseDetail;
import com.dwd.hrm.dto.CourseDto;
import com.dwd.hrm.exception.SystemError;
import com.dwd.hrm.query.CourseDocQuery;
import com.dwd.hrm.query.CourseQuery;
import com.dwd.hrm.service.ICourseService;
import com.dwd.hrm.domain.Course;

import com.baomidou.mybatisplus.plugins.Page;
import com.dwd.hrm.util.AjaxResult;
import com.dwd.hrm.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    public ICourseService courseService;

    


    /**
     * 保存和修改公用的
     *
     * @param courseDto 传递的实体
     * @return Ajaxresult转换结果
     */
    @PreAuthorize("hasAuthority('course:addOrUpdate')")
    @PutMapping("/addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody CourseDto courseDto) {
        try {
            if (courseDto.getCourse().getId() != null)
                courseService.updateById(courseDto);
            else
                courseService.insert(courseDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            courseService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    @PostMapping(value = "/querymarketAndDetal/{id}")
    public AjaxResult querymarketAndDetal(@PathVariable("id") Long id) {
        try {
            CourseDetail courseDetail = courseService.querymarketAndDetal(id);
            return AjaxResult.me().setResultObj(courseDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("查询失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Course get(@PathVariable("id") Long id) {
        return courseService.selectById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping("listAll")
    @PreAuthorize("hasAuthority('course:listAll')")
    public List<Course> list() {

        return courseService.selectList(null);
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Course> json(@RequestBody CourseQuery query) {
        Page<Course> page = new Page<Course>(query.getPage(), query.getRows());
        page = courseService.selectPage(page);
        return new PageList<Course>(page.getTotal(), page.getRecords());
    }

    /**
     * 上线
     *
     * @param course
     * @return
     */
    @PostMapping(value = "/onLineCourse")
    @PreAuthorize("hasAuthority('course:onLineCourse')")
    public AjaxResult onLineCourse(@RequestBody Course course) {
        try {
            AjaxResult ajaxResult = courseService.bathUpOrBathDown(course, true);
            return ajaxResult;
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setStatusCode(SystemError.CODE_400_PARAM_VALID.getCode()).setMessage("上线失败！" + e.getMessage());
        }
    }
   /* @PostMapping(value = "/onLineCourse")
    public AjaxResult onLineCourse(@RequestBody List<Course> courses) {
        try {
            courseService.bathUp(courses);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setStatusCode(SystemError.CODE_400_PARAM_VALID.getCode()).setMessage("上线失败！" + e.getMessage());
        }
    }*/

    /**
     * 下线
     *
     * @param course
     * @return
     */
    @PostMapping(value = "/offLineCourse")
    public AjaxResult offLineCourse(@RequestBody Course course) {
        try {
            AjaxResult ajaxResult = courseService.bathUpOrBathDown(course, false);
            return ajaxResult;
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setStatusCode(SystemError.CODE_400_PARAM_VALID.getCode()).setMessage("下线失败！" + e.getMessage());
        }
    }


    @PostMapping("/queryCourses")
    public AjaxResult queryCourses(@RequestBody CourseDocQuery courseDocQuery) {
        PageList<CourseDoc> courseDocPageList = courseService.queryCourses(courseDocQuery);
        return AjaxResult.me().setResultObj(courseDocPageList);
    }


}
